<?
//display section in functie de id (daca acesta este 0 atunci se adauga):
function display($id)
{
	//incarc sql cu valori pentru titlu:
	$searchTitles = returnQuery("select * from document_body where type = 'Title' order by order_line",$dbh);
	$searchSubtitles = returnQuery("select * from document_body where type = 'Subtitle' order by order_line",$dbh);
	
	$i = 0; 
	$arrTitles = array(); 
	while ($row = mysql_fetch_array($searchTitles)) { 
		$arrTitles[$i] = $row["content"]; 
		$i++; 
	}
	$j = 0; 
	$arrSubtitles = array(); 
	while ($row = mysql_fetch_array($searchSubtitles)) { 
		$arrSubtitles[$j] = $row["content"]; 
		$j++; 
	} 
	?>
	
	<!-- SCRIPT PENTRU POPULAREA COMBOXURILOR -->
	<script type="text/javascript"> 
		arrTitle = new Array(); 
		<?php 
		for($i=0;$i<count($arrTitles); $i++){ 
			echo "arrTitle[$i]='".$arrTitles[$i]."';\n"; 
		} 
		?> 
		arrSubtitle = new Array(); 
		<?php 
		for($i=0;$i<count($arrSubtitles); $i++){ 
			echo "arrSubtitle[$i]='".$arrSubtitles[$i]."';\n"; 
		} 
		?> 

	function umple(idx) {
		var f=document.add_title_form;
		if (idx==0) {
		//f.sel_place.options.length=null;
		//alert(arrSubtitle[0]);
		for(var i=0; i<arrTitle.length; i++) {
			f.sel_place.options[i] = new Option(arrTitle[i]);
		}
		f.sel_place.options[0].selected = true;
		} else {
		for(var i=0; i<arrSubtitle.length; i++) {
			f.sel_place.options[i] = new Option(arrSubtitle[i]);
		}
		//f.sel_place.options[0]=new Option('aa');
		//f.sel_place.options[1]=new Option('vv');
		f.sel_place.options[0].selected = true;
		}
		//for(var i=0; i<10; i++) {
		//	f.sel_place.options[i]=new Option(i, i); 
		//}    
		}
	</script>

	<?
	if ($id==0)
	{
	?>

	<body onload="umple(0);">
	<form action="" name="add_title_form" method="POST">
	<table width="100%">
	<tr><td width="100">Type</td><td><select name="sel_type" style="width:100%" onchange="umple(this.selectedIndex)">
											<option value="Title">Title</option>
											<option value="Subtitle">Subtitle</option></select></td></tr>
	<tr><td>Place after</td><td><select name="sel_place" style="width:100%"></select></td></tr>
	
	<tr><td>With Content</td><td><input type="checkbox" name="content" value="true" checked></td></tr>
	
  
    <tr><td><input type="submit" name="qr_add" value="Adauga" ></td>
	<td align="right"><input type="submit" name="qr_cancel" value="Anulare"></td></tr>
	</table>
	</form>
	</body>
	<?
	}
	else
	{
	//poate reusim sa facem transfer de variabile ca sa nu stresam db'ul...
	$result = returnQuery("select * from companies where id = ".$id.";",'');
	$row = mysql_fetch_assoc($result);
	?>
	<form action="" name="testQR" method="post">
	<table width="100%">
	<tr><td width="100">Nume</td><td><input name="cname" type="text" value="<? echo $row['name'] ?>" style="width:98%"></td></tr>
	<tr><td>CUI</td><td><input name="cui" type="text" value="<? echo $row['cui'] ?>" style="width:98%"></td></tr>
	<tr><td>j</td><td><input name="j" type="text" value="<? echo $row['j'] ?>" style="width:98%"></td></tr>
	<tr><td>Strada</td><td><input name="street" type="text" value="<? echo $row['street'] ?>" style="width:98%"></td></tr>
	<tr><td>Nr</td><td><input name="nr" type="text" value="<? echo $row['nr'] ?>" style="width:98%"></td></tr>
	<tr><td>Bloc</td><td><input name="building" type="text" value="<? echo $row['building'] ?>" style="width:98%"></td></tr>
	<tr><td>Etaj</td><td><input name="floor" type="text" value="<? echo $row['floor'] ?>" style="width:98%"></td></tr>
    <tr><td>Oras</td><td><input name="city" type="text" value="<? echo $row['city'] ?>" style="width:98%"></td></tr>
    <tr><td>Judet</td><td><input name="county" type="text" value="<? echo $row['county'] ?>" style="width:98%"></td></tr>
    <tr><td>Tara</td><td><input name="country" type="text" value="<? echo $row['country'] ?>" style="width:98%"></td></tr>
    <tr><td>Cod postal</td><td><input name="postalcode" type="text" value="<? echo $row['postal_code'] ?>" style="width:98%"></td></tr>
    <tr><td>Banca</td><td><input name="bank" type="text" value="<? echo $row['bank'] ?>" style="width:98%"></td></tr>
	<tr><td>Cont bancar</td><td><input name="bank_account" type="text" value="<? echo $row['bank_account'] ?>" style="width:98%"></td></tr>
	<tr><td><input type="submit" name="qr_update" value="Modifica">
	<input type="submit" name="qr_del" value="Sterge"></td>
	<td align="right"><input type="submit" name="qr_cancel" value="Anulare"></td></tr>
	</table>
	</form>
	<?
	}
}

	function refreshPage($destination)
	{
		print "<meta http-equiv=\"refresh\" content=\"0; url=".$destination."\">";
	 
	}

//Connect to db:
function dbConn()
{
$username = "root";
$password = "";
$hostname = "localhost";	
$handler = mysql_connect($hostname, $username, $password) 
	or die("Unable to connect to MySQL");
//print "Connected to MySQL<br>";

if (!mysql_select_db('ec_methodology', $handler)) {
    echo 'Could not select database';
    exit;
}
return $handler;
}

//DB Close:
function dbClose($handler)
{
	mysql_close($handler)
	or die("Error closing connection. Was the connection open before?");
	//echo "conexiune inchisa cu succes";
}

//query:
function returnQuery($qr,$handler)
{
//SELECT * FROM `projects` WHERE `ID` LIKE '1'
$result = mysql_query($qr);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query. "\n";
    die($message);
	//de adaugat in continuare cum se afiseaza tabelul!!!
}
return $result;
}
?>